package xyz.thoughtset.viewer.modules.fun.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
//import xyz.thoughtset.viewer.common.crud.core.service.BaseServiceImpl;
import xyz.thoughtset.viewer.common.crud.core.service.BaseServiceImpl;
import xyz.thoughtset.viewer.modules.fun.constants.FunParamTypeConstant;
import xyz.thoughtset.viewer.modules.fun.dao.FunInfoDao;
import xyz.thoughtset.viewer.modules.fun.dao.FunParamDao;
import xyz.thoughtset.viewer.modules.fun.entity.FunInfo;
import xyz.thoughtset.viewer.modules.fun.entity.FunParam;
import xyz.thoughtset.viewer.modules.step.entity.QueryBlock;
import xyz.thoughtset.viewer.modules.step.service.QueryBlockService;
//import xyz.thoughtset.viewer.modules.step.entity.QueryBlock;
//import xyz.thoughtset.viewer.modules.step.service.QueryBlockService;

import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class FunInfoServiceImpl extends BaseServiceImpl<FunInfoDao, FunInfo> implements FunInfoService {
    @Autowired
    private FunParamDao funParamDao;
    @Autowired
    private QueryBlockService queryBlockService;

    @Override
    public Object saveData(LinkedHashMap<String, Object> baseMap) {
        FunInfo entity = (FunInfo) super.saveData(baseMap);
        List<FunParam> apiParams = entity.getParams();
        if (!ObjectUtils.isEmpty(apiParams)){
            for (FunParam param : apiParams){
                param.setPid(entity.getId());
                funParamDao.insertOrUpdate(param);
            }
            QueryWrapper<FunParam> paramQuery = new QueryWrapper();
            paramQuery.lambda().eq(FunParam::getPid,entity.getId()).select(FunParam::getId);
            List<FunParam> oldParams = funParamDao.selectList(paramQuery);
            HashSet params = new HashSet<>();
            for (FunParam param : apiParams){
                param.setPid(entity.getId());
                funParamDao.insertOrUpdate(param);
                params.add(param.getId());
            }
            if (!ObjectUtils.isEmpty(oldParams)){
                List<String> delIds = oldParams.stream()
                        .filter(e->!params.contains(e.getId()))
                        .map(FunParam::getId)  // 提取name属性
                        .collect(Collectors.toList());
                funParamDao.deleteByIds(delIds);
            }
        }

        return entity;
    }

    @Override
    public FunInfo getById(Serializable id) {
        FunInfo result = super.getById(id);
        QueryWrapper<FunParam> query = new QueryWrapper();
        query.lambda().eq(FunParam::getPid,id).orderByAsc(FunParam::getOrderNum).orderByDesc(FunParam::getUpdatedAt);
        result.setParams(funParamDao.selectList(query));
        return result;
    }

    @Override
    public boolean deleteById(String pkey) {
        List<QueryBlock> blocks = queryBlockService.listByPId(pkey);
        for (QueryBlock block : blocks){
            queryBlockService.deleteById(block.getId());
        }
        QueryWrapper<FunParam> query = this.pidQueryWrapper(pkey);
        funParamDao.delete(query);
        return super.deleteById(pkey);
    }

    @Override
    public List<FunParam> findFunInputParams(String funId) {
        QueryWrapper<FunParam> query = new QueryWrapper();
        query.lambda()
                .eq(FunParam::getPid,funId)
                .eq(FunParam::getParamType, FunParamTypeConstant.INPUT)
                .orderByAsc(FunParam::getOrderNum)
                .orderByDesc(FunParam::getUpdatedAt);
        return funParamDao.selectList(query);
    }

    @Override
    public List<FunInfo> findAIToolsWithInParam(String serverId) {
        List<FunInfo> list = baseMapper.findAIToolsWithInParam(serverId);
        return list;
    }
}
